package Client;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

public class ClientP {
	private final Connection connection;
	private final Session session;
	private final MessageProducer producer;
	/**
	 * Constructeur
	 * 
	 * @param factory
	 * @param topicName
	 * @throws JMSException
	 */
	public ClientP(final Connection factory, final String topicName)
			throws JMSException {
		connection = factory;
		this.session = this.connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		final Destination destination = this.session.createTopic(topicName);
		this.producer = this.session.createProducer(destination);
	}
	/**
	 * Fermeture de connexion
	 * 
	 * @throws JMSException
	 */
	public void close() throws JMSException {

		if (this.connection != null) {
			this.connection.close();
		}
	}
	/**
	 * Envoi du message
	 * 
	 * @throws JMSException
	 */
	public void run(String unMessage) throws JMSException {

		final Message message = this.session.createTextMessage(unMessage);
		this.producer.send(message);
	}
}
